<?php

namespace app\admin\controller;

use think\Controller;

class Article extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!session('name')){
            $this->redirect("/admin/login/login");
        }
        $this->article=new \app\common\model\Article();
    }
    public function index(){
        $artList=$this->article->select();
        //$artList['examfile']=$this->article->where('art_type',2)->select();
        $this->assign('artList',$artList);
        return $this->fetch();
    }
    public function add(){
        if(request()->isPost()){
            $data=input('param.');
            $checkData=new \app\admin\Validate\Article();
            if(!$checkData->check($data)){
                return $this->error($checkData->getError());
            }else{
                $res=$this->article->saveArt($data);
                if($res['valid']){
                    return $this->success($res['msg'],'admin/article/index');
                }else{
                    return $this->error($res['msg']);
                }
            }
        }
        return $this->fetch();
    }
    public function edit(){
        if(request()->isPost()){
            $data=input('param.');
            $checkData=new \app\admin\Validate\Article();
            if(!$checkData->check($data)){
                return $this->error($checkData->getError());
            }else{
                $res=$this->article->saveArt($data);
                if($res['valid']){
                    return $this->success($res['msg'],'admin/article/index');
                }else{
                    return $this->error($res['msg']);
                }
            }
        }
        $artId=input('param.id');
        $art=$this->article->where('art_id',$artId)->find();
        $this->assign('art',$art);
        return $this->fetch();
    }
    public function delete(){
        $artId=input('param.id');
        $res=$this->article->where("art_id",$artId)->delete();
        if($res){
            return $this->success('删除成功');
        }else{
            return $this->error("删除失败");
        }
    }

}
